/**
 * 时间戳转日期格式
 * @param {string/number} timestamp
 * @param {string} format
 * @returns
 */
export const formatDate = (timestamp, format) => {
  const date = new Date(parseInt(timestamp) * 1000);

  //年
  let y = date.getFullYear();

  //月
  let m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;

  //日
  let d = date.getDate();
  d = d < 10 ? "0" + d : d;

  //时
  let h = date.getHours();
  h = h < 10 ? "0" + h : h;

  //分
  let f = date.getMinutes();
  f = f < 10 ? "0" + f : f;

  //秒
  let s = date.getSeconds();
  s = s < 10 ? "0" + s : s;

  //根据格式化字符串返回结果
  let result;
  switch (format) {
    case "yyyy-MM-dd":
      result = `${y}-${m}-${d}`;
      break;
    case "yyyy/MM/dd":
      result = `${y}/${m}/${d}`;
      break;
    case "yyyy-MM-dd HH:mm:ss":
      result = `${y}-${m}-${d} ${h}:${f}:${s}`;
      break;
    default:
      result = `${y}-${m}-${d} ${h}:${f}:${s}`;
  }

  return result;
};
